﻿package ascript.components.ai
{
	//#########################
	// Header Includes
	//#########################
	import ascript.Game;
	import Box2D.Common.Math.b2Vec2;
	import ascript.GameComponent;
	import ascript.GameLevel;
	import ascript.GameObject;
	import ascript.utilities.PointSimple;
	
	//#########################
	// AIHoming Class
	//#########################
	public class AIHoming extends GameComponent
	{
		//=========================
		// Constructors
		//=========================	
		public function AIHoming():void
		{
		}
		
		//=========================
		// Members
		//=========================	
		override public function initComponent( tGame:Game, tObject:GameObject ):void
		{
			// Set Default Attributes
			defaultAttribute( tObject, "HomingIsActive", false );
			defaultAttribute( tObject, "HomingIsArrivalStopped", false );
			defaultAttribute( tObject, "HomingTarget", "" );
			defaultAttribute( tObject, "HomingUpdateDelay", 100 );
			defaultAttribute( tObject, "HomingTimer", 0 );
		}
				
		override public function updateComponent( tGame:Game, tObject:GameObject, tLevel:GameLevel ):void
		{
			if ( tObject.getAttribute( "HomingIsActive" ) )
			{				
				if ( ( tGame.time - tObject.getAttribute( "HomingTimer" ) ) >= tObject.getAttribute( "HomingUpdateDelay" ) )
				{
					var tempDirection:b2Vec2 = ( ( tLevel.getObject( tObject.getAttribute( "HomingTarget" ) ) ).getAttribute( "Location" ) ).Copy();
					tempDirection.Subtract( tObject.getAttribute( "Location" ) );
					
					tObject.setAttribute( "HomingTimer", tGame.time );
					
					if ( tempDirection.Length() < 50 && tObject.getAttribute( "HomingIsArrivalStopped" ) )
					{
						tObject.setAttribute( "Direction", new b2Vec2() );
						tObject.setAttribute( "HomingIsActive", false );
					}
					else
					{
						tempDirection.Normalize();
						tObject.setAttribute( "Direction", tempDirection );
					}
				}
			}
		}
	}
}